package com.shop.cereshop.admin.controller;

import com.shop.cereshop.admin.annotation.NoRepeatSubmit;
import com.shop.cereshop.admin.annotation.NoRepeatWebLog;
import com.shop.cereshop.admin.page.report.ReportDetailVO;
import com.shop.cereshop.admin.page.report.ReportRecordPageVO;
import com.shop.cereshop.admin.param.report.ReportAuditParam;
import com.shop.cereshop.admin.param.report.ReportHandleParam;
import com.shop.cereshop.admin.param.report.ReportPageParam;
import com.shop.cereshop.admin.service.report.CereReportRecordService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.user.CerePlatformUser;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 举报功能管理控制器
 *
 * @author
 */
@RestController
@RequestMapping("report")
@Slf4j(topic = "ReportController")
@Api(value = "举报功能管理", tags = "举报功能管理")
public class ReportController {

    @Autowired
    private CereReportRecordService cereReportRecordService;

    /**
     * 分页查询举报记录
     */
    @PostMapping("page")
    @ApiOperation(value = "分页查询举报记录")
    public Result getReportPage(@RequestBody ReportPageParam param, HttpServletRequest request) {
        CerePlatformUser user = (CerePlatformUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }
        try {
            Page<ReportRecordPageVO> page = cereReportRecordService.getReportPage(param);
            return Result.success(page);
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("查询举报记录失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

    /**
     * 获取举报详情
     */
    @GetMapping("detail")
    @ApiOperation(value = "获取举报详情")
    public Result getReportDetail(@ApiParam(value = "举报ID", required = true) @RequestParam("reportId") Long reportId,
                                 HttpServletRequest request) {
        CerePlatformUser user = (CerePlatformUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            ReportDetailVO detail = cereReportRecordService.getReportDetail(reportId);
            return Result.success(detail);
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取举报详情失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

    /**
     * 审核举报
     */
    @PostMapping("audit")
    @NoRepeatSubmit
    @NoRepeatWebLog
    @ApiOperation(value = "审核举报")
    public Result auditReport(@Validated @RequestBody ReportAuditParam param, HttpServletRequest request) {
        CerePlatformUser user = (CerePlatformUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            Boolean result = cereReportRecordService.auditReport(param, user);
            return Result.success(result);
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("审核举报失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

    /**
     * 处理举报
     */
    @PostMapping("handle")
    @NoRepeatSubmit
    @NoRepeatWebLog
    @ApiOperation(value = "处理举报")
    public Result handleReport(@Validated @RequestBody ReportHandleParam param, HttpServletRequest request) {
        CerePlatformUser user = (CerePlatformUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            Boolean result = cereReportRecordService.handleReport(param, user,request);
            return Result.success(result);
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("处理举报失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

    /**
     * 获取举报统计信息
     */
    @GetMapping("stats")
    @ApiOperation(value = "获取举报统计信息")
    public Result getReportStats(HttpServletRequest request) {
        CerePlatformUser user = (CerePlatformUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            Object stats = cereReportRecordService.getReportStats();
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取举报统计信息失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }
}
